<?php
session_start();
/**
 * Created by PhpStorm.
 * User: Pico_Dragon
 * Date: 15/12/2014
 * Time: 10:53
 */
include "../../db/dbConnect.php";
include '../function/pagging.php';

error_reporting(E_ALL && ~ E_NOTICE);
$p=new Pager();
$p->setLimit(15);
$p->findStart($p->getLimit());

if($_SERVER['REQUEST_METHOD'] == 'GET' && $_GET['keyword']) {
    $keyword = mysql_real_escape_string($_GET['keyword']);
    $and = " and  q.question LIKE '%" . $keyword . "%'";
}

    $sql="select q.*, k.nameKind as kind
          from u_profile_question as q, u_quest_kind as k
          WHERE q.kind=k.idkind
          AND q.deleted=0".$and;

$sql2=$sql;
$result=mysql_query($sql);
if(mysql_error()){
    echo mysql_error();
    exit();
}

$count=0;
$count=mysql_num_rows($result);
$p->findPages($count, $p->getLimit());

$start=$p->getStart();
$limit=$p->getLimit();
$sql2.=' limit '.$start.','.$limit.'';

$result=mysql_query($sql2);
if(mysql_error()){
    echo mysql_error();
    exit();
}
?>

<table class="listQ w100p">
            <thead>
            <th class="tl"><input type="checkbox" name="all" id="all" chosen="0" title="Chọn tất cả"></th>
            <th class="tc ">STT</th>
            <th class="tc w50p">Nhãn</th>
            <th class="tc w20p">Lượt trả lời</th>
            <th class="tc w15p">Loại câu hỏi</th>
            <th class="tc w5p"></th>
            </thead>
            <div class="clear"></div>
            <tbody>
            <?php
            $i=0;
            while($row=mysql_fetch_array($result)) {
                $i++;
                $oe=(($i % 2)==1)?'o':'e';
                ?>
                <tr class="<?php echo $oe;?>" title="click để xem chi tiết">
                    <td class="tl"><input type="checkbox" class="one" name="one" id="<?php echo $row['idpr_question']?>" title="Chọn"></td>
                    <td class="tc" style="border-left: 1px solid #ccc;"><?php echo $i;?></td>
                    <td class="tl click2detail" id="<?php echo $row['idpr_question']?>" style="border-left: 1px solid #ccc;"><?php echo $row['question'];?>

                    </td>
                    <td class="tc" style="border-left: 1px solid #ccc;"><?php echo '-'; ?></td>
                    <td class="tc" style="border-left: 1px solid #ccc;"><?php
                        echo $row['kind'];
                        ?>
                    </td>
                    <td class="tr"  style="border-left: 1px solid #ccc;">
                        <button id="del" class="btn btn-small del" idu="<?php echo $row['idpr_question'];?>">Xóa</button>


                    </td>
                </tr>

                <div class="clear"></div>
                <?php
                unset($active);
            }
            if($i==0){
                echo '<tr>
                        <td colspan="6">Không có bản ghi nào!</td>
                    </tr>';
            }
            ?>
            <tr>
                <td colspan="6">
                    <table width="100%" border="0" cellspacing="0" cellpadding="0" bgcolor="#66FF66">
                        <tr>
                            <td align="center" valign="middle">Từ <?php echo($p->getStart()+1).' đến '.((($end=($p->getStart()+$p->getLimit()))>$count)?$count:$end).' trong tổng cộng '.$count.'';?></td>
                            <td align="center" valign="middle"><?php $page=$p->pageList($_GET["page"],$p->getPage(),"web/admin/questionSearch.php");
                                echo $page;
                                ?></td>
                        </tr>
                    </table>
                </td>
            </tr>
</tbody>
</table>

<script type="text/javascript">
    $(".del").bind('click', function(e){
        if(confirm("Bạn có chắc chắn  muốn xóa câu hỏi này?")){
            e.preventDefault();
            var idu = $(this).attr('idu'), page=$("#pag_nav").text();
            $.ajax({
                url:"web/admin/questionDel.php",
                type:"POST",
                data: 'idq='+idu,
                success:function(re){
                    $("#tbList").load("web/admin/questionSearch.php?page="+page).fadeIn('slow');
                    $(".result").hide().text(re).slideDown('slow').fadeOut(4000);
                }
            })
        }
    });
    $('.click2Detail').bind('click',function(e){
        e.preventDefault();
        var id=$(this).attr('id');
        $("#loadDetail").load("web/admin/questionDetail.php?question="+id).fadeIn();
    });

    $("#multi-del").bind('click', function(e){
        e.preventDefault();
        var checked=false;
        var idu, i=0, page=$("#pag_nav").text();
       // alert("full:"+$(this).attr('full')+' num:'+$(this).attr('num'));
        if(!$(this).attr('num') && !$(this).attr('full') ){
            alert("Hãy chọn ít nhất một bản ghi!");
        }else{
            $(".one.checked").each(function(){
                i++;
                idq=$(this).attr('id');
                $.ajax({
                    url:"web/admin/questionDel.php",
                    type:"POST",
                    data:'idu='+idu,
                    success:function(re){
                        $("#tbList").load("web/admin/questionSearch.php?page="+page).fadeIn('slow');
                        $curr.attr('num','0');
                        $curr.attr('full','0');
                    }
                })
            })
            if(i)$(".result").hide().text(re+' '+i+' bản ghi!').slideDown('slow').fadeOut(4000);
        }

    });




    $("#all").click(function(){
        var checked_status = this.checked;
        $(".one").each(function(){
            this.checked = checked_status;
            if($(this).is(':checked')){
                $("#multi-del").attr('full','1');//danh dau tren nut xoa nhieu la da chon het
                $("#multi-active").attr('full','1');//danh dau tren nut xoa nhieu la da chon het
                $(this).addClass('checked').removeClass('unchecked');
            }else{
                $("#multi-del").attr('full','0');//danh dau tren nut xoa nhieu la chua chon ban ghi nao
                $("#multi-del").attr('num',0);
                $("#multi-active").attr('full','0');//danh dau tren nut xoa nhieu la chua chon ban ghi nao
                $("#multi-active").attr('num',0);
                $(this).addClass('unchecked').removeClass('checked');
            }
        });
    });
    $(".one").click(function(){
        if($(this).is(':checked')){
            var num=$("#multi-del").attr('num');
            num++;
            $("#multi-del").attr('num',num);
            $("#multi-active").attr('num',num);
            $(this).addClass('checked').removeClass('unchecked');
        }else{
            $(this).addClass('unchecked').removeClass('checked');
            var num=$("#multi-del").attr('num');
            num--;
            $("#multi-del").attr('num',num);
            $("#multi-active").attr('num',num);
        }

        if($('input[name=all]').is(':checked')){

            $('input[name=all]').prop('checked', false);
            $("#multi-del").attr('full','0');
            $("#multi-active").attr('full','0');
        }
    });


    $('a.pag_nav').click(function(){
        var $href=$(this).attr('href');
        //alert($href);
        $(" #tbList").load($href);
        return false;
    });
</script>